/*****************************************************************************
 * Copyright (c) 2010 -, Anders All rights reserved
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 *  Contact: Anders Uddenberg (anders.uddenberg@gmail.com)
 * 
 *
 ****************************************************************************/
package org.easypr.organizer.client.view;

import java.util.ArrayList;

import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

import org.easypr.organizer.client.presenter.ImportPresenter;
import org.easypr.organizer.shared.MyContact;
import org.easypr.organizer.shared.MyUser;

// TODO: Auto-generated Javadoc
/**
 * The Class MenuView.
 */
public class ImportView extends Composite implements ImportPresenter.Display {

	/** The container. */
	FlowPanel container;
	
	/** The boxes. */
	ArrayList<CheckBox> boxes;
	
	/** The contacts. */
	ArrayList<MyContact> contacts;
	
	/** The import link. */
	Anchor importLink;
	
	/** The next link. */
	Anchor nextLink;
	
	/** The save status label. */
	Label saveStatusLabel;

	/**
	 * Instantiates a new menu view.
	 */
	public ImportView() {
		container = new FlowPanel();
		boxes = new ArrayList<CheckBox>();
		importLink = new Anchor("Import selected");
		nextLink = new Anchor("Get more contacts");
		saveStatusLabel = new Label();
		initWidget(container);
	}

	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.ImportPresenter.Display#getImportLink()
	 */
	public Anchor getImportLink() {
		return this.importLink;
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.ImportPresenter.Display#getNextLink()
	 */
	public Anchor getNextLink() {
		return this.nextLink;
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.ImportPresenter.Display#getBoxes()
	 */
	public ArrayList<CheckBox> getBoxes() {
		return this.boxes;
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.ImportPresenter.Display#getContacts()
	 */
	public ArrayList<MyContact> getContacts() {
		return this.contacts;
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.ImportPresenter.Display#getSaveStatusLabel()
	 */
	public Label getSaveStatusLabel() {
		return this.saveStatusLabel;
	}
	
	
	
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.ImportPresenter.Display#setData(org.easypr.organizer.shared.MyUser, java.util.ArrayList)
	 */
	public void setData(MyUser user, ArrayList<MyContact> contacts) {

		container.clear();
		
		FlexTable bodycontent = new FlexTable();
		bodycontent.setWidth("100%");
		bodycontent.setCellSpacing(2);
		
		HorizontalPanel header = new HorizontalPanel();
		header.setStyleName("content-header");
		
		FlowPanel body = new FlowPanel();
		body.setStyleName("content-body");

		Label lbl = new Label("Import contacts from Google");
		lbl.setStyleName("header-label");
		
		header.add(lbl);
		container.add(header);
		
		
		if(contacts != null && contacts.size() > 0) {
			this.contacts = contacts;
			this.boxes.clear();
			for(int i = 0; i<contacts.size(); i++) {
				MyContact contact = contacts.get(i);
				CheckBox box = new CheckBox(contact.getName());
				box.getElement().setId(Integer.toString(i)); // Used for matching box to contact 
				if(contact.getName().length() == 0) {
					box.setEnabled(false);
				}
				boxes.add(box);
				bodycontent.setWidget(i, 0, box);
				Label emailLabel = new Label(contact.getEmail());
				emailLabel.setStyleName("email");
				bodycontent.setWidget(i, 1, emailLabel);
				bodycontent.setWidget(i, 2, new Label(contact.getWorkplace()));
				bodycontent.setWidget(i, 3, new Label(contact.getWorkphone()));
			}
			bodycontent.setWidget(contacts.size(), 0, importLink);
			bodycontent.setWidget(contacts.size(), 3, nextLink);
			bodycontent.setWidget(contacts.size()+1, 0, saveStatusLabel);
			
		} else {
			bodycontent.setWidget(0, 0, new Label("No contacts found!"));
		}
		container.add(bodycontent);
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.ImportPresenter.Display#asWidget()
	 */
	public Widget asWidget() {
		return this;
	}
	
}
